home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 183 / dpcs0503.iso / Components / Microsoft ASP / _SETUP.1 / ASPWizard.jar / asp / wizard / util / UiUtil.class (.txt) < prev   
Encoding:
Java Class File  |  1998-11-20  |  10.5 KB  |  304 lines

  1. package asp.wizard.util;
  2.  
  3. import asp.netobjects.nfx.ui.OrderedListModel;
  4. import com.sun.java.swing.BorderFactory;
  5. import com.sun.java.swing.DefaultListModel;
  6. import com.sun.java.swing.JComboBox;
  7. import com.sun.java.swing.JComponent;
  8. import com.sun.java.swing.JLabel;
  9. import com.sun.java.swing.JScrollPane;
  10. import com.sun.java.swing.JTextArea;
  11. import com.sun.java.swing.ListModel;
  12. import com.sun.java.swing.UIManager;
  13. import com.sun.java.swing.table.DefaultTableModel;
  14. import com.sun.java.swing.table.TableModel;
  15. import com.sun.java.swing.text.JTextComponent;
  16. import java.awt.Color;
  17. import java.awt.Component;
  18. import java.awt.Container;
  19. import java.awt.Dimension;
  20. import java.awt.Font;
  21. import java.awt.GridBagConstraints;
  22. import java.awt.GridBagLayout;
  23. import java.awt.Insets;
  24. import java.awt.Toolkit;
  25. import java.lang.reflect.Array;
  26. import java.util.Enumeration;
  27. import java.util.Vector;
  28.  
  29. public class UiUtil {
  30.    private static boolean _lafSetupSuccess;
  31.    private static String _lafOrigName = null;
  32.    // $FF: synthetic field
  33.    static Class class$java$lang$Object;
  34.  
  35.    private UiUtil() {
  36.    }
  37.  
  38.    public static void addComponent(Container cont, Component comp, GridBagLayout gbl, GridBagConstraints gbc, int x, int y, int width, int height, double weightx, double weighty) {
  39.       if (gbl == cont.getLayout()) {
  40.          gbc.gridx = x;
  41.          gbc.gridy = y;
  42.          gbc.gridwidth = width;
  43.          gbc.gridheight = height;
  44.          gbc.weightx = weightx;
  45.          gbc.weighty = weighty;
  46.          cont.add(comp, gbc);
  47.       }
  48.    }
  49.  
  50.    public static void addComponent(Container cont, Component comp, GridBagLayout gbl, GridBagConstraints gbc, int x, int y, int width, int height, double weightx, double weighty, int anchor, int fill) {
  51.       gbc.anchor = anchor;
  52.       gbc.fill = fill;
  53.       addComponent(cont, comp, gbl, gbc, x, y, width, height, weightx, weighty);
  54.    }
  55.  
  56.    public static void addComponent(Container cont, Component comp, GridBagLayout gbl, GridBagConstraints gbc, int x, int y, int width, int height, double weightx, double weighty, int anchor, int fill, int insettop, int insetleft, int insetbottom, int insetright) {
  57.       setInsets(gbc.insets, insettop, insetleft, insetbottom, insetright);
  58.       addComponent(cont, comp, gbl, gbc, x, y, width, height, weightx, weighty, anchor, fill);
  59.    }
  60.  
  61.    public static final void setInsets(Insets i, int top, int left, int bottom, int right) {
  62.       i.top = top;
  63.       i.left = left;
  64.       i.bottom = bottom;
  65.       i.right = right;
  66.    }
  67.  
  68.    public static final JLabel setupLabel(JLabel l, Component comp) {
  69.       l.setFont(new Font("Dialog", 1, 11));
  70.       ((Component)l).setForeground(new Color(102, 102, 153));
  71.       if (comp != null) {
  72.          l.setLabelFor(comp);
  73.       }
  74.  
  75.       return l;
  76.    }
  77.  
  78.    public static final JScrollPane setupTextAreaAsMultilineLabel(JTextArea txa, Color bgcolor, int columns, String text) {
  79.       txa.setLineWrap(true);
  80.       txa.setWrapStyleWord(true);
  81.       ((JTextComponent)txa).setText(text);
  82.       txa.setColumns(columns);
  83.       ((JTextComponent)txa).setEditable(false);
  84.       ((JComponent)txa).setBorder(BorderFactory.createEmptyBorder());
  85.       ((Component)txa).setBackground(bgcolor);
  86.       txa.setFont(new Font("Dialog", 0, 11));
  87.       JScrollPane scp = new JScrollPane(txa, 21, 31);
  88.       ((JComponent)scp).setBorder(BorderFactory.createEmptyBorder());
  89.       return scp;
  90.    }
  91.  
  92.    public static void addItemToComboBox(Vector items, JComboBox comboBox) {
  93.       if (items != null && comboBox != null) {
  94.          Enumeration e = items.elements();
  95.  
  96.          while(e.hasMoreElements()) {
  97.             comboBox.addItem(e.nextElement());
  98.          }
  99.       }
  100.  
  101.    }
  102.  
  103.    public static Object[] getColumnArrayFromTableModel(int colIndex, TableModel tableModel, Class columnClass) {
  104.       Object[] result = null;
  105.       if (columnClass == null) {
  106.          columnClass = class$java$lang$Object != null ? class$java$lang$Object : (class$java$lang$Object = class$("java.lang.Object"));
  107.       }
  108.  
  109.       if (tableModel != null && colIndex >= 0 && colIndex < tableModel.getColumnCount()) {
  110.          int rowCount = tableModel.getRowCount();
  111.          if (rowCount > 0) {
  112.             Object[] aColumn = null;
  113.  
  114.             try {
  115.                aColumn = Array.newInstance(columnClass, rowCount);
  116.             } catch (Exception e) {
  117.                System.err.println("UiUtil.getColumnArrayFromTableModel(): " + ((Throwable)e).getMessage());
  118.             }
  119.  
  120.             if (aColumn != null) {
  121.                for(int r = 0; r < rowCount; ++r) {
  122.                   aColumn[r] = tableModel.getValueAt(r, colIndex);
  123.                }
  124.  
  125.                result = aColumn;
  126.             }
  127.          }
  128.       }
  129.  
  130.       return result;
  131.    }
  132.  
  133.    public static void setTableModelFromColumnArray(int colIndex, DefaultTableModel tableModel, Object[] array) {
  134.       if (tableModel != null && array != null) {
  135.          int colCount = tableModel.getColumnCount();
  136.          if (colIndex >= 0 && colIndex < colCount) {
  137.             int rowCount = Array.getLength(array);
  138.             tableModel.setNumRows(rowCount);
  139.  
  140.             for(int r = 0; r < rowCount; ++r) {
  141.                tableModel.setValueAt(array[r], r, colIndex);
  142.             }
  143.          }
  144.       }
  145.  
  146.    }
  147.  
  148.    public static boolean valueExistsInTableModel(int colIndex, DefaultTableModel tableModel, String value) {
  149.       if (tableModel != null && value != null) {
  150.          for(int i = 0; i < tableModel.getRowCount(); ++i) {
  151.             if (value.compareTo(tableModel.getValueAt(i, colIndex).toString()) == 0) {
  152.                return true;
  153.             }
  154.          }
  155.       }
  156.  
  157.       return false;
  158.    }
  159.  
  160.    public static boolean valueExistsInListModel(DefaultListModel ListModel, String value) {
  161.       if (ListModel != null && value != null) {
  162.          for(int i = 0; i < ListModel.size(); ++i) {
  163.             if (value.compareTo((String)ListModel.elementAt(i)) == 0) {
  164.                return true;
  165.             }
  166.          }
  167.       }
  168.  
  169.       return false;
  170.    }
  171.  
  172.    public static String formatDialogText(String MyVal) {
  173.       int StartLoc = 0;
  174.       int EndLoc = 0;
  175.       int width = 60;
  176.       String NewStr = "";
  177.       if (MyVal != null) {
  178.          int TotLength = MyVal.length();
  179.  
  180.          for(String LnBrk = "\n"; EndLoc < TotLength; StartLoc = EndLoc) {
  181.             EndLoc = MyVal.indexOf(" ", width + StartLoc);
  182.             if (EndLoc == -1) {
  183.                EndLoc = TotLength;
  184.                LnBrk = "";
  185.             }
  186.  
  187.             NewStr = NewStr + MyVal.substring(StartLoc, EndLoc) + LnBrk;
  188.          }
  189.       }
  190.  
  191.       return NewStr;
  192.    }
  193.  
  194.    public static int getListModelIndexByString(ListModel lm, String str) {
  195.       int result = -1;
  196.       if (lm != null && str != null) {
  197.          int size = lm.getSize();
  198.  
  199.          for(int i = 0; i < size && result == -1; ++i) {
  200.             Object el = lm.getElementAt(i);
  201.             if (str.equals(el.toString())) {
  202.                result = i;
  203.             }
  204.          }
  205.       }
  206.  
  207.       return result;
  208.    }
  209.  
  210.    public static Vector strToVector(String MyVal) {
  211.       Vector result = new Vector();
  212.       int StartLoc = 0;
  213.  
  214.       for(int EndLoc = MyVal.indexOf("|", StartLoc); EndLoc != -1; EndLoc = MyVal.indexOf("|", StartLoc)) {
  215.          result.addElement(MyVal.substring(StartLoc, EndLoc));
  216.          StartLoc = EndLoc + 1;
  217.       }
  218.  
  219.       result.addElement(MyVal.substring(StartLoc, MyVal.length()));
  220.       return result;
  221.    }
  222.  
  223.    public static void centerComponentInScreen(Component comp) {
  224.       Dimension currSize = comp.getSize();
  225.       Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  226.       comp.setLocation(screenSize.width / 2 - currSize.width / 2, screenSize.height / 2 - currSize.height / 2);
  227.    }
  228.  
  229.    public static void populateCombo(JComboBox combo, Vector list) {
  230.       Enumeration values = list.elements();
  231.       combo.removeAllItems();
  232.  
  233.       while(values.hasMoreElements()) {
  234.          combo.addItem((String)values.nextElement());
  235.       }
  236.  
  237.    }
  238.  
  239.    public static void populateCombo(JComboBox combo, OrderedListModel list) {
  240.       Enumeration values = ((DefaultListModel)list).elements();
  241.       combo.removeAllItems();
  242.  
  243.       while(values.hasMoreElements()) {
  244.          combo.addItem((String)values.nextElement());
  245.       }
  246.  
  247.    }
  248.  
  249.    public static void validateTableAgainstList(DefaultTableModel theDTM, DefaultListModel theDLM, int columnToCompare) {
  250.       int rc = theDTM.getRowCount();
  251.       if (rc > 0) {
  252.          for(int i = rc - 1; i >= 0; --i) {
  253.             boolean bSaveRow = false;
  254.  
  255.             for(int j = 0; j < theDLM.getSize(); ++j) {
  256.                String lmElement = (String)theDLM.getElementAt(j);
  257.                String tmValue = (String)theDTM.getValueAt(i, columnToCompare);
  258.                if (lmElement.compareTo(tmValue) == 0) {
  259.                   bSaveRow = true;
  260.                }
  261.             }
  262.  
  263.             if (!bSaveRow) {
  264.                theDTM.removeRow(i);
  265.             }
  266.          }
  267.       }
  268.  
  269.    }
  270.  
  271.    public static void lafSetup() {
  272.       _lafSetupSuccess = false;
  273.  
  274.       try {
  275.          _lafOrigName = UIManager.getLookAndFeel().getClass().getName();
  276.          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  277.          _lafSetupSuccess = true;
  278.       } catch (Exception var0) {
  279.       }
  280.  
  281.    }
  282.  
  283.    public static void lafRestore() {
  284.       if (_lafSetupSuccess) {
  285.          try {
  286.             UIManager.setLookAndFeel(_lafOrigName);
  287.             _lafSetupSuccess = false;
  288.          } catch (Exception e) {
  289.             System.err.println(((Throwable)e).getMessage());
  290.          }
  291.       }
  292.  
  293.    }
  294.  
  295.    // $FF: synthetic method
  296.    static Class class$(String class$) {
  297.       try {
  298.          return Class.forName(class$);
  299.       } catch (ClassNotFoundException forName) {
  300.          throw new NoClassDefFoundError(((Throwable)forName).getMessage());
  301.       }
  302.    }
  303. }
  304.